import MySql.Goods;
import MySql.goodsManager;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class ShowGoodsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        createCookies("1","1",request,response);
        createCookies("2","3",request,response);
        createCookies("3","5",request,response);
        createCookies("4","7",request,response);
        createCookies("5","9",request,response);
        createCookies("6","11",request,response);
        createCookies("7","13",request,response);
        createCookies("8","15",request,response);
        createCookies("9","17",request,response);
        createCookies("10","19",request,response);
        createCookies("11","21",request,response);
        createCookies("12","23",request,response);
//        response.sendRedirect("showtest.jsp");
        response.sendRedirect("setintroduce.go");
    }

    public void createCookies(String no,String id,HttpServletRequest request, HttpServletResponse response){
        try {
            Goods good=goodsManager.getRowById(id);
            Cookie cookie1=new Cookie("item-"+no+"-id",good.getId());
            Cookie cookie2=new Cookie("item-"+no+"-kind",good.getKind());
            Cookie cookie3=new Cookie("item-"+no+"-title",good.getTitle());
            Cookie cookie4=new Cookie("item-"+no+"-brief",good.getBrief());
            Cookie cookie5=new Cookie("item-"+no+"-price",good.getPrice().toString());

            cookie1.setMaxAge(60*60*60*60);
            cookie2.setMaxAge(60*60*60*60);
            cookie3.setMaxAge(60*60*60*60);
            cookie4.setMaxAge(60*60*60*60);
            cookie5.setMaxAge(60*60*60*60);

            cookie1.setPath(request.getContextPath() + "/");
            cookie2.setPath(request.getContextPath() + "/");
            cookie3.setPath(request.getContextPath() + "/");
            cookie4.setPath(request.getContextPath() + "/");
            cookie5.setPath(request.getContextPath() + "/");
            response.addCookie(cookie1);
            response.addCookie(cookie2);
            response.addCookie(cookie3);
            response.addCookie(cookie4);
            response.addCookie(cookie5);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}


